package com.ujiuye.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ujiuye.config.*;
import com.ujiuye.mapper.*;
import com.ujiuye.pojo.*;
import com.ujiuye.service.ISetmealService;
import com.ujiuye.vo.BusinessVo;
import com.ujiuye.vo.SetmealCheckgroupVo;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 中间表  一个套餐有什么检查组 服务实现类
 * </p>
 *
 * @author zs
 * @since 2021-07-05
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements ISetmealService {
    @Resource
    private SetmealMapper setmealMapper;
    @Resource
    private SetmealCheckgroupMapper setmealCheckgroupMapper;
    @Resource
    private CheckgroupMapper checkgroupMapper;
    @Resource
    private CheckitemMapper checkitemMapper;
    @Resource
    private CheckgroupCheckitemMapper checkgroupCheckitemMapper;


    @Override
    public PageResult listPage(QueryPageBean queryPageBean) {
        Page<Setmeal> page = new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        String queryString = queryPageBean.getQueryString();
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper();
        if(queryString!=null && queryString.length()>0){
            queryWrapper.eq("code",queryString)
                    .or().eq("name",queryString)
                    .or().eq("helpcode",queryString);
        }
        Page<Setmeal> checkgroupPage = setmealMapper.selectPage(page, queryWrapper);
        PageResult pageResult = new PageResult(checkgroupPage.getTotal(),checkgroupPage.getRecords());
        return pageResult;
    }

    @Override
    public void save(Integer[] checkgroupIds, Setmeal setmeal) {
         setmealMapper.insert(setmeal);
        for (Integer checkgroupId : checkgroupIds) {
            SetmealCheckgroup setmealCheckgroup = new SetmealCheckgroup(setmeal.getId(),checkgroupId);
            setmealCheckgroupMapper.insert(setmealCheckgroup);
        }
    }

    @Override
    public Result getById(int id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        QueryWrapper<SetmealCheckgroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("setmeal_id",setmeal.getId());
        List<SetmealCheckgroup> setmealCheckgroups = setmealCheckgroupMapper.selectList(queryWrapper);
        List<Integer> checkgroupIds = new ArrayList<>();
        for (SetmealCheckgroup setmealCheckgroup : setmealCheckgroups) {
            checkgroupIds.add(setmealCheckgroup.getCheckgroupId());
        }
        SetmealCheckgroupVo setmealCheckgroupVo = new SetmealCheckgroupVo(setmeal,checkgroupIds);
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,setmealCheckgroupVo);
    }

    @Override
    public void update(Integer[] checkgroupIds, Setmeal setmeal) {
        setmealMapper.updateById(setmeal
                           );
        QueryWrapper<SetmealCheckgroup> queryWrapper = new QueryWrapper();
        queryWrapper.eq("setmeal_id",setmeal.getId());
        setmealCheckgroupMapper.delete(queryWrapper);
        for (Integer checkgroupId : checkgroupIds) {
            SetmealCheckgroup setmealCheckgroup = new SetmealCheckgroup(setmeal.getId(),checkgroupId);
            setmealCheckgroupMapper.insert(setmealCheckgroup);
        }
    }

    @Override
    public Setmeal getSetMeal(int id) {
        //查出套餐
        Setmeal setmeal = setmealMapper.selectById(id);
        QueryWrapper<SetmealCheckgroup> setmealCheckgroupQueryWrapper = new QueryWrapper<>();
        setmealCheckgroupQueryWrapper.eq("setmeal_id",id);
        //通过套餐ID查出中间表查出所有的检查组
        List<SetmealCheckgroup> setmealCheckgroupList = setmealCheckgroupMapper.selectList(setmealCheckgroupQueryWrapper);
        List<Checkgroup> checkgroupList = new ArrayList<>();//用于存检查组
        //遍历所有检查组
        if (setmealCheckgroupList!=null && setmealCheckgroupList.size()>0){
            for (SetmealCheckgroup setmealCheckgroup : setmealCheckgroupList) {
                //通过中间表检查组的ID查出每一个检查组
                Checkgroup checkgroup = checkgroupMapper.getById(setmealCheckgroup.getCheckgroupId());
                QueryWrapper<CheckgroupCheckitem> checkgroupCheckitemQueryWrapper = new QueryWrapper<>();
                checkgroupCheckitemQueryWrapper.eq("checkgroup_id",checkgroup.getId());
                //构造条件在通过检查组的ID查检查组和检查项的中间表
                List<CheckgroupCheckitem> checkgroupCheckitemList = checkgroupCheckitemMapper.selectList(checkgroupCheckitemQueryWrapper);
                //每一个检查组有多个检查项  创建他用于存放检查项
                List<Checkitem> checkitemList = new ArrayList<>();
                if (checkgroupCheckitemList!=null && checkgroupCheckitemList.size()>0){
                    //遍历中间表数据通过中间表检查项的ID查出每一个检查项
                    for (CheckgroupCheckitem checkgroupCheckitem : checkgroupCheckitemList) {
                        Checkitem checkitem = checkitemMapper.getById(checkgroupCheckitem.getCheckitemId());
                        //将检查项放入集合
                        checkitemList.add(checkitem);
                    }
                    //一个检查组对应多个检查项
                    checkgroup.setCheckitemList(checkitemList);
                }
                //将所有检查组存入集合
                checkgroupList.add(checkgroup);
            }
        }
        //一个套餐对应多个检查组
        setmeal.setCheckgroupList(checkgroupList);

        return setmeal;
    }

    @Override
    public List<Map<String, Object>> listSetMealCount() {
        List<Map<String, Object>> maps = setmealMapper.listSetMealCount();
        return maps;
    }


}
